package com.shandao.shangxiu.ui;

import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.LinearLayout;

import com.shandao.shangxiu.R;
import com.shandao.shangxiu.fragment.BusinessFragment;
import com.shandao.shangxiu.fragment.HomeFragment;
import com.shandao.shangxiu.fragment.MeFragment;
import com.shandao.shangxiu.fragment.NetHotFragment;


/**
 * 主页面
 * @author king
 */
public class MainActivity extends com.shandao.shangxiu.base.BaseActivity {
	private static final int TAB_HOME = 0;
	private static final int TAB_BUSINESS = 1;
	private static final int TAB_NETNOT = 2;
	private static final int TAB_ME = 3;

	private LinearLayout ll_home;
	private LinearLayout ll_business;
	private LinearLayout ll_nethot;
	private LinearLayout ll_me;

	private FragmentManager fragmentManager;
	private HomeFragment fragmentHome;  // 主页
	private BusinessFragment fragmentBusiness;// 商户页
	private NetHotFragment fragmentNetHot;// 网红页
	private MeFragment fragmentMe;// 个人中心页

	private static long back_pressed; // 点击返回键的时间值


	@Override
	protected int getLayoutId() {
		return R.layout.activity_main;
	}

	@Override
	protected boolean hasToolbar() {
		return false;
	}

	@Override
	public void initView() {
		ll_home = (LinearLayout) findViewById(R.id.ll_home);
		ll_business = (LinearLayout) findViewById(R.id.ll_business);
		ll_nethot = (LinearLayout) findViewById(R.id.ll_nethot);
		ll_me = (LinearLayout) findViewById(R.id.ll_me);

		ll_home.setOnClickListener(this);
		ll_business.setOnClickListener(this);
		ll_nethot.setOnClickListener(this);
		ll_me.setOnClickListener(this);

	}

	@Override
	public void initData() {
		fragmentManager = getSupportFragmentManager();
		ll_home.performClick();
	}



	@Override
	public void onClick(View v) {// 底部按钮的选择
		int id = v.getId();
		if (id == R.id.ll_home) {
			setTabSelection(TAB_HOME);
		}else if (id == R.id.ll_business) {
			setTabSelection(TAB_BUSINESS);
		}else if (id == R.id.ll_nethot) {
			setTabSelection(TAB_NETNOT);
		}else if (id == R.id.ll_me) {
			setTabSelection(TAB_ME);
		}
	}


	// 切换Fragment
	private void setTabSelection(int position) {
		// 更改底部导航栏按钮状态
		changeButtonStatus(position);
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		// 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
		hideFragments(transaction);
		switch (position) {
			case TAB_HOME:
				if (fragmentHome == null) {
					fragmentHome = new HomeFragment();
					transaction.add(R.id.realtabcontent, fragmentHome);
				} else {
					transaction.show(fragmentHome);
				}
				break;
			case TAB_BUSINESS:
				if (fragmentBusiness == null) {
					fragmentBusiness = new BusinessFragment();
					transaction.add(R.id.realtabcontent, fragmentBusiness);
				} else {
					transaction.show(fragmentBusiness);
				}
				break;
			case TAB_NETNOT:
				if (fragmentNetHot == null) {
					fragmentNetHot = new NetHotFragment();
					transaction.add(R.id.realtabcontent, fragmentNetHot);
				} else {
					transaction.show(fragmentNetHot);
				}
				break;
			case TAB_ME:
				if (fragmentMe == null) {
					fragmentMe = new MeFragment();
					transaction.add(R.id.realtabcontent, fragmentMe);
				} else {
					transaction.show(fragmentMe);
				}
				break;
		}
		transaction.commitAllowingStateLoss();
	}

	// 根据position更改底部按钮状态
	private void changeButtonStatus(int position) {
		switch (position) {
			case TAB_HOME:
				ll_home.setSelected(true);
				ll_business.setSelected(false);
				ll_nethot.setSelected(false);
				ll_me.setSelected(false);
				break;
			case TAB_BUSINESS:
				ll_home.setSelected(false);
				ll_business.setSelected(true);
				ll_nethot.setSelected(false);
				ll_me.setSelected(false);
				break;
			case TAB_NETNOT:
				ll_home.setSelected(false);
				ll_business.setSelected(false);
				ll_nethot.setSelected(true);
				ll_me.setSelected(false);
				break;
			case TAB_ME:
				ll_home.setSelected(false);
				ll_business.setSelected(false);
				ll_nethot.setSelected(false);
				ll_me.setSelected(true);
				break;
		}
	}

	// 隐藏所有Fragment
	private void hideFragments(FragmentTransaction transaction) {
		if (fragmentHome != null) {
			transaction.hide(fragmentHome);
		}
		if (fragmentBusiness != null) {
			transaction.hide(fragmentBusiness);
		}
		if (fragmentNetHot != null) {
			transaction.hide(fragmentNetHot);
		}
		if (fragmentMe != null) {
			transaction.hide(fragmentMe);
		}
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
	}

	@Override
	public void onBackPressed() {
		if (back_pressed + 2000 > System.currentTimeMillis()) {
			// 退出程序
			super.onBackPressed();
		} else {
			showToast("再次点击退出");
		}
		back_pressed = System.currentTimeMillis();
	}
}
